﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Hotel.Domain;
using HOTEL.Dao;
using Hotel_Admin.Shared;
namespace Hotel_Admin.Common
{
    public class Authentication:Page
    {
        protected GroupPerDao grDao = new GroupPerDao();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Authenticate();
        }
        public void Authenticate()
        {
            if (UserLogin == null)
            {
                Redirect("Login.aspx");
            }
        }
        public TAI_KHOAN UserLogin
        {
            get
            {
                return Session[SessionKey.SessionLogin] as TAI_KHOAN;
            }
            set
            {
                Session[SessionKey.SessionLogin] = value;
            }
        }
        public void Redirect(String Url)
        {
            Response.Redirect(Url,true);
        }
        public bool HasViewPermission(int PageId)
        {
            if (UserLogin == null) return false;
            var has = grDao.HasPermission(1, PageId, UserLogin.MaNV);
            return has;
        }
        public bool HasInsertPermission(int PageId)
        {
            if (UserLogin == null) return false;
            var has = grDao.HasPermission(2, PageId, UserLogin.MaNV);
            return has;
        }
        public bool HasDeletePermission(int PageId)
        {
            if (UserLogin == null) return false;
            var has = grDao.HasPermission(3, PageId, UserLogin.MaNV);
            return has;
        }
        public bool HasUpdatePermission(int PageId)
        {
            if (UserLogin == null) return false;
            var has = grDao.HasPermission(4, PageId, UserLogin.MaNV);
            return has;
        }
        public int? CurrentPage
        {
            get
            {
                return int.Parse(Session[SessionKey.SessionCurrentPage].ToString());
            }
            set
            {
                Session[SessionKey.SessionCurrentPage] = value;
            }
        }
        public enum Permission
        {
            /// <summary>
            /// View
            /// </summary>
            Read=1,
            /// <summary>
            /// Insert
            /// </summary>
            Insert=2,
            /// <summary>
            /// Delete
            /// </summary>
            Delete=3,
            /// <summary>
            /// Update
            /// </summary>
            Update=4
        }
        #region Startup script registeration
        public void ShowError(string message, string controlId)
        {
            ((SiteMaster)Page.Master).ShowError(message, controlId);
        }

        public void ShowError(string message)
        {
            ((SiteMaster)Page.Master).ShowError(message);
        }

        public void ShowInfor(string message)
        {
            ((SiteMaster)Page.Master).ShowInfor(message);
        }

        public void UnblockDialog(string divId)
        {
            var master = (SiteMaster)Page.Master;
            if (master != null) master.UnblockDialog(divId);
        }

        public void CloseWaitingDialog()
        {
            ((SiteMaster)Page.Master).CloseWaitingDialog();
        }

        public void SetReadonly(string id, bool isReadonly)
        {
            ((SiteMaster)Page.Master).SetReadonly(id, isReadonly);
        }

        public void SetControlValue(string id, string value)
        {
            ((SiteMaster)Page.Master).SetControlValue(id, value);
        }

        public void HideDialog(string divId)
        {
            ((SiteMaster)Page.Master).HideDialog(divId);
        }

        public void SetLabel(string id, string value)
        {
            var master = (SiteMaster)Page.Master;
            if (master != null) master.SetLabel(id, value);
        }

        public void ShowWarning(string message)
        {
            var master = (SiteMaster)Page.Master;
            if (master != null) master.ShowWarning(message);
        }

        public void UnblockWaitingDialog()
        {
            var master = (SiteMaster)Page.Master;
            if (master != null) master.UnblockWaitingDialog();
        }

        public void FocusAndSelect(string controlId)
        {
            ((SiteMaster)Page.Master).FocusAndSelect(controlId);
        }
        #endregion
    }
}